package com.example.servlet;
import com.example.dao.NovelDao;
import com.example.dao.UserDao;
import com.example.domain.Novel;
import com.example.domain.Review;
import com.example.domain.User;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
@WebServlet("/ReviewUpdateStatuServlet")
public class ReviewUpdateStatuServlet extends HttpServlet{
    public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
    {
        doPost(request,response);
    }
    public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
    {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        int id = Integer.parseInt(request.getParameter("reviewId"));
        int status_admin = Integer.parseInt(request.getParameter("status_admin"));
        NovelDao novelDao=new NovelDao();
        Review review = novelDao.getReviewById(id);
        //                根据小说的名字查询小说表中是否有该小说
        if(novelDao.isNovel(review.getTitle())){
            response.getWriter().write("novelFailure");
            novelDao.updateReviewStatus(id,3);
            return;
        }
        int affectedRow = novelDao.updateReviewStatus(id,status_admin);
        if(affectedRow>0){
            if(status_admin==1){
//                把刚添加的小说添加到小说表中
                Novel novel = new Novel(review.getTitle(),review.getAuthor(),review.getDescription(),review.getCover_image_url(),review.getCategory(),review.getStatu_s(),review.getUpdate_time(),0);
                affectedRow = novelDao.addNovel(novel);
                if (affectedRow>0){
                    response.getWriter().write("novelSuccess");
                }else{
                    response.getWriter().write("novelMessageFail");
                }
            }else{
                response.getWriter().write("success");
            }
        }else{
            response.getWriter().write("数据库更新失败");
        }

    }
}
